<?php
/**
 * Display Inbox message.
 * Simple Grid type of report.
 *
 * @author Xavier VILAIN
 * 
 */
use Fox\Dao\SqlFragment;

use Fox\Html\Screen;
use Fox\Note;

$idusr = User::current()->getId();
$xview = Xview::instance();

$edit = null;

$screen = new Screen("eNotes");
$grid = new Fox\Grid('G_INBOX');

$query = \Fox\Dao::infos(Note::className())->handle()->createQueryBuilder();
$query->select('n.notid','n.subject','n.sdate','u.notid AS iread','u.loekz')
	->from('notes','n')
	->leftJoin('n','note_status','u', " n.notid = u.notid AND u.idusr = '$idusr' ");

$fragments[] = new SqlFragment("target = ? ",array($idusr));
	
if($xview->val("show_all") == true)
{
	
}
else
{
	$fragments[] = new SqlFragment("NOT (u.idusr = ? AND u.loekz = ?) OR u.notid IS NULL OR (u.idusr = ? AND u.loekz IS NULL )",
											array($idusr,1,$idusr));
}
$fragment = SqlFragment::combine($fragments);
$result = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);

if(count($result))
{
	// Load Notes Data
	$toolbar = new Fox\Html\ToolBar("tb");
	$current = $xview->currentURL();
	$ilink = "?mod=notes&act=delete";
	$toolbar->add(new Fox\Html\Button("idele","GridLinesAction('G_INBOX','$ilink','X','S','Refresh(\'$current\')');"));
	$showAll = $xview->val('show_all',false);
	if($showAll)
	{
		$toolbar->add(new Fox\Html\Button("ishow","?mod=notes","Masquer Supprimer"));
	}else
	{
		$toolbar->add(new Fox\Html\Button("ishow","?mod=notes&show_all=true",Msg(Fox\Msg::BUNDLE,Fox\Msg::DELETED_MESSAGES)));
	}
}

$unread = 0;
if(is_array($result))
{
	foreach($result as $key => $detail)
	{
		
		if(empty($detail['iread']) || $detail['loekz'] != 0)
		{
			$result[$key]['iread'] = " bold ";
			$unread ++;
		}
		else
			$result[$key]['iread'] = null;
	}
}


$grid->fieldcat(array(array('title','subject'),array('udate','sdate'),'iread','notid'));
$grid->fieldcat()->field("iread")->noOutput();
$grid->setFieldAttr('notid','SetHiddenInput',true);
$grid->setFieldAttr('notid','SetHidden',true);
$grid->setFieldAttr('notid','SetClass',"notid");
//$anchor = new Fox\Html\Anchor("?mod=notes&act=iread",array('notid'));
$anchor = new Fox\Html\Anchor("Display('P_NOTE_DETAIL','?mod=notes&act=message#ilink');",array('notid'));
$grid->setFieldAttr(array('subject'),"setAnchor",$anchor);
$grid->setCoche();
$grid->fieldcat()->setClassField("iread");
$grid->fieldcat()->behaviors(Fox\Behaviors::key(Fox\Generator::useElement('notid')));
$grid->fieldcat()->setNoDataMessage(Msg(Fox::MSGCL,50));
$grid->SetDatas($result);

echo "<h1>Messages</h1><br>";
if(isset($toolbar))
echo $toolbar->display();
echo $grid->display();